package resp

import (
	"net/http"
	"time"

	"github.com/gin-gonic/gin"
)

const (
	EventClose = "close"
	EventError = "error"
	EventData  = "data"
)

// SSEEvent 将连接切换为 SSE 事件流
func SetupSSE(c *gin.Context, timeout time.Duration) error {
	writer := c.Writer

	responseController := http.NewResponseController(writer)
	if err := responseController.SetWriteDeadline(time.Now().Add(timeout)); err != nil {
		return RESTErrSetDeadline
	}

	writer.Header().Set("Content-Type", "text/event-stream, charset=utf-8")
	writer.Header().Set("Cache-Control", "no-cache")
	writer.Header().Set("Connection", "keep-alive")

	return nil
}
